package com.hanlin.backend.service.impl;

import com.hanlin.backend.common.entity.HlUser;
import com.hanlin.backend.common.entity.HlUserLoginLog;
import com.hanlin.backend.common.util.HttpUtil;
import com.hanlin.backend.mapper.HlUserLoginLogMapper;
import com.hanlin.backend.service.HlUserLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 登录记录表 服务实现类
 * </p>
 *
 * @author hl.yuan
 * @since 2021-04-20
 */
@Service
@Slf4j
public class HlUserLoginLogServiceImpl extends ServiceImpl<HlUserLoginLogMapper, HlUserLoginLog> implements HlUserLoginLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLoginLog(String code, HlUser hlUser, HttpServletRequest request) {
        HlUserLoginLog loginLog = new HlUserLoginLog();
        if (hlUser!=null) {
            loginLog.setUserId(hlUser.getId());
        }
        loginLog.setLoginState(Integer.valueOf(code));
        loginLog.setLoginTime(new Date());
        loginLog.setLoginType(0);
        loginLog.setLoginIp(HttpUtil.getIpAddress(request));
        loginLog.setUpdateTime(new Date());
        save(loginLog);
    }


}
